import logging

import pytest

from read_data.read_data import *
logger = logging.getLogger(__name__)

class AssertTool:

    def __init__(self,resp):
        self.resp=resp

    def assert_true(self,casedata):
        # for i in casedata:
            for a in casedata["assertFields"]:
                self.field = a["field"]
                self.expect = a["expect"]
                self.compare_type = a["type"]
                actual  = eval(f'{self.resp}{self.field}')

                #断言
                if self.compare_type == 'equals':
                    if actual == self.expect:
                        return True
                    else:
                        logger.error(f"断言失败{actual}不等于{self.expect}")
                        raise AssertionError("")
                elif self.compare_type == "contains":
                     if str(actual) in str(self.expect):
                        return True
                     else:
                        logger.error(f"断言失败{actual}不包含{self.expect}")
                        raise AssertionError("")
                elif self.compare_type == "ISNOTNull":
                     if bool(actual) :
                         return True
                     else:
                         logger.error(f"断言失败{actual}为空")
                         raise AssertionError("")
                elif self.compare_type == "type":
                     if isinstance(actual, eval(self.expect)):
                         return True
                     else:
                         logger.error(f"断言失败类型{actual}不等于{eval(self.expect)}")
                         raise AssertionError("")
                elif self.compare_type == "length":
                     if len(actual) == int(self.expect):
                         return True
                     else:
                         logger.error(f"断言失败长度{len(actual)}不等于{int(self.expect)}")
                         raise AssertionError("")
                else:
                    raise ValueError(f"不支持的比较类型: {self.compare_type}")
